﻿using System;
using System.Collections;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
using FlickrMe.shortcut;

namespace FlickrMe
{
    /**
     * Static methods for working with filenames.
     */
    class Utility
    {
        

        public static long getTotalFileByteSize(string filename)
        {
            return getTotalFileByteSize(new String[] { filename });
        }

        public static long getTotalFileByteSize(string[] filenames)
        {
            long bytes = 0;

            foreach (String filename in filenames)
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(filename);
                bytes += fi.Length;
            }
            return bytes;
        }

        /*
         * 
         */
        public static String[] getImageFilenamesFromFolders(String[] fileSytemNames)
        {
            List<String> l = new List<String>(4);
            foreach (String name in fileSytemNames)
            {
                l.Add(name);
            }
            l = stripImageFilenamesFromFolder(l);

            return (String[])l.ToArray();
        }

        /*
         * returns a list of all filenames contained in a folder that are of a supported image type.
         */
        public static List<String> stripImageFilenamesFromFolder(List<String> filenames)
        {
            // Get a shortcut resolver suitable for the operating environement.
            ShortcutResolver resolver = ShortcutResolverFactory.getResolver();

            List<String> list = new List<String>(5);
            foreach (String filename in filenames)
            {
                
                String resolved = filename;

                // check if the filename is a shortcut and resolve it if need be.
                if (resolver != null && resolver.isShortcut(filename))
                {
                   resolved = resolver.resolvePath(filename); 
                }
                
                // Check if filename is a directory

                DirectoryInfo d = new DirectoryInfo(resolved);
                
                if (d.Exists)
                {
                    // call this method recursevley for each item in the directory
                    FileSystemInfo[] finfo = d.GetFileSystemInfos();
                    list.AddRange(stripImageFilenamesFromFolder(FileNamesFromFileInfo(finfo)));
                }
                else
                {
                    // Check if the non-directory file is of a supported image type and add it to the list.
                    if (isImageFile(resolved))
                    {
                        list.Add(resolved);
                    }
                    
                }
            }
            return list;
        }

    
        /*
         * Returns filesnames as an array of String from an array of FileSystemInfo.
         */
        public static List<String> FileNamesFromFileInfo(FileSystemInfo[] finfo)
        {
            List<String> names = new List<String>(finfo.Length);
           
            foreach (FileSystemInfo info in finfo)
            {
                names.Add(info.FullName);

            }
            return names;
        }

        /*
         * Determines if a filename has an image extension.
         * 
         */
        public static bool isImageFile(String name)
        {
            String ext = new FileInfo(name).Extension.ToLower();

            if (ext.Equals(".jpg") || ext.Equals(".jpeg") || ext.Equals(".gif")
                    || ext.Equals(".png") || ext.Equals(".tiff"))
            {
                return true;
            }

            return false;
        }
    }
}
